package com.teamsun.util;

import com.teamsun.rptcfg.BaseRptColumn;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>将request请求中的参数转换为map</p>
 */
public class RequestParamToMap {

    /**
     * 将request中的参数转换为Map
     * @param request  http请求
     * @param conditionCol 查询列
     * @return
     */
    public static Map<String, String> convertToMap(HttpServletRequest request, List<BaseRptColumn> conditionCol) {
        // 获取请求中的参数信息
        Map<String,String[]> requestParam = request.getParameterMap();
        Map<String, String> returnMap = new HashMap<String, String>();

        // 获取查询参数中的日期参数
        Map<String, BaseRptColumn> dateMap = new HashMap<String, BaseRptColumn>();
        Map<String, BaseRptColumn> rptColumnMap = new HashMap<String, BaseRptColumn>();
        if(conditionCol != null && conditionCol.size() > 0) {
            for(BaseRptColumn rptColumn : conditionCol) {
                String conditionType = rptColumn.getConditionType();
                if(Constants.CONDITIONTYPE_DATE.equals(conditionType)) {
                    dateMap.put(rptColumn.getAliasField(), rptColumn);
                } else if(Constants.CONDITIONTYPE_DATE_RANGE.equals(conditionType)) {
                    dateMap.put(rptColumn.getAliasField() + "_start", rptColumn);
                    dateMap.put(rptColumn.getAliasField() + "_end", rptColumn);
                } else {
                    if(Constants.CONDITIONTYPE_VALUE_RANGE.equals(conditionType)) {
                        rptColumnMap.put(rptColumn.getAliasField() + "_start", rptColumn);
                        rptColumnMap.put(rptColumn.getAliasField() + "_end", rptColumn);
                    } else {
                        rptColumnMap.put(rptColumn.getAliasField(), rptColumn);
                    }
                }
            }
        }

        // 循环遍历请求参数，并保存至Map中
        if(requestParam != null && requestParam.size() > 0) {
            for(Map.Entry<String, String[]> entry : requestParam.entrySet()) {
                String paramName = entry.getKey();
                String paramValue = "";  // 参数值
                Object value = entry.getValue();

                // 多选下拉框传入参数名时，会添加[],在此需要将[]删除
                if(paramName.endsWith("[]")) {
                    paramName = paramName.substring(0, paramName.length() - 2);
                }
                if(value == null) {
                    paramValue = "";
                } else if(value instanceof String[]){
                    String[] values = (String[]) value;
                    if(values.length > 1) {
                        for (int i = 0; i < values.length; i++) {
                            paramValue = paramValue + "\'" + values[i] + "\',";
                        }
                        if (paramValue.endsWith(",")) {
                            paramValue = paramValue.substring(0, paramValue.length() - 1);
                        }
                    } else {
                        paramValue = values[0];

                        if(paramName.startsWith("hid_")) {
                            String splitValue = "";
                            String[] tmpValues = paramValue.split(",");
                            for(String tmpValue : tmpValues) {
                                if(!tmpValue.startsWith("\'") && !tmpValue.endsWith("\'")) {
                                    tmpValue = "\'" + tmpValue + "\',";
                                    splitValue = splitValue + tmpValue;
                                } else if(tmpValue.startsWith("\'") && !tmpValue.endsWith("\'")) {
                                    tmpValue = tmpValue + "\',";
                                    splitValue = splitValue + tmpValue;
                                } else if(!tmpValue.startsWith("\'") && tmpValue.endsWith("\'")) {
                                    tmpValue = "\'" + tmpValue + ",";
                                    splitValue = splitValue + tmpValue;
                                } else {
                                    splitValue = splitValue + tmpValue + ",";
                                }
                            }
                            if(splitValue.endsWith(",")) {
                                splitValue = splitValue.substring(0, splitValue.length() - 1);
                            }
                            paramValue = splitValue;
                        }

                        // 如果参数是日期类型，需要将参数值格式化
                        BaseRptColumn rptColumn = dateMap.get(paramName);
                        if(rptColumn != null) {
                            if(StringUtils.isNotBlank(paramValue)) {
                                paramValue = formatDate(paramValue, rptColumn, request);
                            }
                        }

                        // 根据数字的格式化方式重置查询参数(例如:数字的格式化方式为万计,则需要将查询条件中输入的参数乘以10000)
                        BaseRptColumn conditiontColumn = rptColumnMap.get(paramName);
                        if(conditiontColumn != null) {
                            String numberFormat = conditiontColumn.getNumberFormat();  // 数字格式化方式
                            String dynamicFormat = conditiontColumn.getDynamicFormat();  // 动态格式化方式
                            if(StringUtils.isNotBlank(numberFormat) || StringUtils.isNotBlank(dynamicFormat)) {
                                paramValue = formatNumberData(conditiontColumn, paramValue, request);
                            }
                        }
                    }
                } else {
                    paramValue = value.toString();

                    if(paramName.startsWith("hid_")) {
                        String splitValue = "";
                        String[] values = paramValue.split(",");
                        for(String tmpValue : values) {
                            if(!tmpValue.startsWith("\'") && !tmpValue.endsWith("\'")) {
                                tmpValue = "\'" + tmpValue + "\',";
                                splitValue = splitValue + tmpValue;
                            } else if(tmpValue.startsWith("\'") && !tmpValue.endsWith("\'")) {
                                tmpValue = tmpValue + "\',";
                                splitValue = splitValue + tmpValue;
                            } else if(!tmpValue.startsWith("\'") && tmpValue.endsWith("\'")) {
                                tmpValue = "\'" + tmpValue + ",";
                                splitValue = splitValue + tmpValue;
                            } else {
                                splitValue = splitValue + tmpValue + ",";
                            }
                        }
                        if(splitValue.endsWith(",")) {
                            splitValue = splitValue.substring(0, splitValue.length() - 1);
                        }
                        paramValue = splitValue;
                    }
                    // 如果参数是日期类型，需要将参数值格式化
                    BaseRptColumn rptColumn = dateMap.get(paramName);
                    if(rptColumn != null) {
                        if(StringUtils.isNotBlank(paramValue)) {
                            paramValue = formatDate(paramValue, rptColumn, request);
                        }
                    }

                    // 根据数字的格式化方式重置查询参数(例如:数字的格式化方式为万计,则需要将查询条件中输入的参数乘以10000)
                    BaseRptColumn conditiontColumn = rptColumnMap.get(paramName);
                    if(conditiontColumn != null) {
                        String numberFormat = conditiontColumn.getNumberFormat();  // 数字格式化方式
                        String dynamicFormat = conditiontColumn.getDynamicFormat();  // 动态格式化方式
                        if(StringUtils.isNotBlank(numberFormat) || StringUtils.isNotBlank(dynamicFormat)) {
                            paramValue = formatNumberData(conditiontColumn, paramValue, request);
                        }
                    }
                }

                returnMap.put(paramName, paramValue);
            }
        }

        return returnMap;
    }

    /**
     * 如果参数是日期类型，将参数值格式化
     * @param paramValue 传入参数
     * @param rptColumn
     * @return 格式化后参数
     */
    public static String formatDate(String paramValue, BaseRptColumn rptColumn, HttpServletRequest request) {
        String resultValue = "";
        if(rptColumn != null && StringUtils.isNotBlank(paramValue)) {
            String pageFormatter = rptColumn.getFormatter();  // 页面日期格式化方式
            String dbFormatter = rptColumn.getDbFormatter();  // 数据库日期格式化方式
            String dynamicFormatter = rptColumn.getDynamicFormat();  // 日期是否动态格式化

            if(StringUtils.isNotBlank(dynamicFormatter)) {
                resultValue = paramValue;
            } else {
                if(StringUtils.isBlank(dbFormatter)) {
                    dbFormatter = pageFormatter;
                }

                // 如果页面日期格式和数据库日期格式不同，则需要转化
                if(StringUtils.isNotBlank(dbFormatter) || !dbFormatter.equals(pageFormatter)) {
                    SimpleDateFormat pageDateFormatter = new SimpleDateFormat(pageFormatter);
                    SimpleDateFormat dbDateFormatter = new SimpleDateFormat(dbFormatter);

                    try {
                        Date pageDate = pageDateFormatter.parse(paramValue);
                        resultValue = dbDateFormatter.format(pageDate);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        return resultValue;
    }

    /**
     * 格式化数字
     * @param paramValue 传入参数
     * @return
     */
    public static String formatNumberData(BaseRptColumn conditiontColumn, String paramValue, HttpServletRequest request) {
        if(conditiontColumn != null && StringUtils.isNotBlank(paramValue)) {
            String numberFormat = conditiontColumn.getNumberFormat();  // 数字格式化方式
            String dynamicFormat = conditiontColumn.getDynamicFormat();  // 动态格式化方式
            if(StringUtils.isNotBlank(dynamicFormat)) {
                // 获取选中的格式化方式
                String formatter = request.getParameter("formatType");
                if(StringUtils.isNotBlank(formatter)) {
                    if(formatter.indexOf(".") > 0) {
                        Double dblValue = Double.valueOf(paramValue);
                        int nodeLength = formatter.length() - 2;
                        if(nodeLength == 1) {
                            dblValue = dblValue * 10;
                        } else if(nodeLength == 2) {
                            dblValue = dblValue * 100;
                        } else if(nodeLength == 3) {
                            dblValue = dblValue * 1000;
                        } else if(nodeLength == 4) {
                            dblValue = dblValue * 10000;
                        }
                        paramValue = String.valueOf(dblValue);
                    }
                }
            } else if(StringUtils.isNotBlank(numberFormat)) {
                if(StringUtils.isNotBlank(numberFormat)) {
                    if(numberFormat.indexOf(".") > 0) {
                        Double dblValue = Double.valueOf(paramValue);
                        int nodeLength = numberFormat.length() - 2;
                        if(nodeLength == 1) {
                            dblValue = dblValue * 10;
                        } else if(nodeLength == 2) {
                            dblValue = dblValue * 100;
                        } else if(nodeLength == 3) {
                            dblValue = dblValue * 1000;
                        } else if(nodeLength == 4) {
                            dblValue = dblValue * 10000;
                        }
                        paramValue = String.valueOf(dblValue);
                    } else if(Constants.NUMBERFORMATER_PERCENT_100.equals(numberFormat)) {
                        Double dblValue = Double.valueOf(paramValue);
                        paramValue = String.valueOf(dblValue / 100);
                    }
                }
            }
        }

        return paramValue;
    }
}
